package com.xjrsoft.module.customerTwo.AppManage.service_support_order.stateMachine;


import com.xjrsoft.module.customerTwo.AppManage.service_support_order.entity.ServiceSupportOrder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.statemachine.StateMachineContext;
import org.springframework.statemachine.StateMachinePersist;
import org.springframework.statemachine.config.EnableStateMachine;
import org.springframework.statemachine.config.EnumStateMachineConfigurerAdapter;
import org.springframework.statemachine.config.StateMachineConfigurerAdapter;
import org.springframework.statemachine.config.builders.StateMachineConfigurationConfigurer;
import org.springframework.statemachine.config.builders.StateMachineStateConfigurer;
import org.springframework.statemachine.config.builders.StateMachineTransitionConfigurer;
import org.springframework.statemachine.listener.StateMachineListener;
import org.springframework.statemachine.listener.StateMachineListenerAdapter;
import org.springframework.statemachine.persist.DefaultStateMachinePersister;
import org.springframework.statemachine.support.DefaultStateMachineContext;
import org.springframework.statemachine.transition.Transition;

import java.util.EnumSet;

/**
 * @author by lisen
 * @Version 1.0.0
 * @Date 2024-04-24 10:27
 * @projectName: xjrsoft_new
 * @package: com.xjrsoft.module.customerTwo.AppManage.service_support_order.stateMachine
 * @className: ServiceSupportStateMachineConfig
 */
@Configuration
@EnableStateMachine(name="ServiceSupportStateMachine")
public class ServiceSupportStateMachineConfig extends StateMachineConfigurerAdapter<ServiceSupportStates,ServiceSupportEvents> {

    @Override
    public void configure(StateMachineStateConfigurer<ServiceSupportStates, ServiceSupportEvents> states) throws Exception {
        states.withStates()
                .initial(ServiceSupportStates.TODO) //定义了初始状态为TODO
                .states(EnumSet.allOf(ServiceSupportStates.class)); //指定States中的所有状态作为该状态机的状态定义
    }

    // 初始化当前状态机有哪些状态迁移动作
    // 有来源状态为source，目标状态为target，触发事件为event
    @Override
    public void configure(StateMachineTransitionConfigurer<ServiceSupportStates, ServiceSupportEvents> transitions)
            throws Exception {
        transitions
                .withExternal()
                .source(ServiceSupportStates.TODO).target(ServiceSupportStates.DONE)
                .event(ServiceSupportEvents.HANDLE) //服务商处理事件将触发：待处理状态->已处理状态
                .and()
                .withExternal()
                .source(ServiceSupportStates.DONE).target(ServiceSupportStates.CLOSED)
                .event(ServiceSupportEvents.CONFIRM); // 确认事件触发，已处理->结案
    }

    @Bean
    public DefaultStateMachinePersister persister(){
        return new DefaultStateMachinePersister<>(new StateMachinePersist<Object, Object, ServiceSupportOrder>() {
            @Override
            public void write(StateMachineContext<Object, Object> context, ServiceSupportOrder order) throws Exception {
                //此处并没有进行持久化操作
            }

            @Override
            public StateMachineContext<Object, Object> read(ServiceSupportOrder order) throws Exception {
                //此处直接获取order中的状态，其实并没有进行持久化读取操作
                return new DefaultStateMachineContext(order.getState(),null,null,null);
            }
        });
    }
/*
    // 初始化当前状态机配置
    @Override
    public void configure(StateMachineConfigurationConfigurer<ServiceSupportStates, ServiceSupportEvents> config)
            throws Exception {
        config.withConfiguration().listener(listener()); // 设置监听器
    }

    // 监听器实例
    @Bean
    public StateMachineListener<ServiceSupportStates, ServiceSupportEvents> listener() {
        return new StateMachineListenerAdapter<ServiceSupportStates, ServiceSupportEvents>() {

            // 在状态机的状态转换时调用
            @Override
            public void transition(Transition<ServiceSupportStates, ServiceSupportEvents> transition) {
                // 当前是未支付状态
                if(transition.getTarget().getId() == ServiceSupportStates.TODO) {
                    System.out.println("客户提交工单");
                    return;
                }

                // 从未支付->待收货状态
                if(transition.getSource().getId() == ServiceSupportStates.TODO
                        && transition.getTarget().getId() == ServiceSupportStates.DONE) {
                    System.out.println("服务商已处理");
                    return;
                }

                // 从待收货->完成状态
                if(transition.getSource().getId() == ServiceSupportStates.DONE
                        && transition.getTarget().getId() == ServiceSupportStates.CLOSED) {
                    System.out.println("用户已确认");
                }
            }
        };
    }*/
}
